setwd("")



library(dplyr)
library(countrycode)
library(reshape2)
library(plm)
library(texreg)
library(ggplot2)
library(scales)
library(ggeffects)
library(sjPlot)
library(sjmisc)


d1<-read.csv("wvs_dem_pub.csv",header=T,sep=",")
names(d1)
d1$V2f<-factor(d1$V2)
d1$yearf<-factor(d1$year)
d1$dem_go_imp_varimax_tenberge2PC_positive<-d1$dem_go_imp_varimax_tenberge2PC+3


### Figure 1

var_dv_ind<-c("dem_go_imp_varimax_tenberge2PC_positive")
dv_ind<-d1[var_dv_ind]

dvind <- ggplot(dv_ind, aes(dem_go_imp_varimax_tenberge2PC_positive,
                            fill = factor(dem_go_imp_varimax_tenberge2PC_positive)))
dvind + geom_bar(aes(y = (..count..)/sum(..count..)),width = 0.15) + 
  scale_y_continuous(labels = function(x) paste0(x*100, "%"))+theme_bw()+
  scale_x_continuous(breaks= pretty_breaks(10))+
  scale_fill_grey(start = 0.95, end = 0.05,name="none")+
  labs(x = "Support for Democracy", y = "Percent of respondents")+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.text.y = element_text(face="plain", vjust=0.5,hjust = 0,
                                   size=12, angle=0),
        axis.title.x=element_text(size=14, margin = margin(t = 20, r = 0, b = 0, l = 0)),
        axis.title.y=element_text(size=14,face="plain"),
        legend.position = "none")
ggsave("Figure1.pdf",width = 5,height = 5)
dev.off()


### Figure 2, data for maps
library(countrycode)
vars<-c("country","dem_import","dem_good","dem_go_imp_varimax_tenberge2PC_positive")
d1_map<-d1[vars]
d1_map$iso3c<-countrycode(d1_map$country, "country.name","iso3c",  warn = T)
vars2<-c("iso3c","dem_import","dem_good",
         "dem_go_imp_varimax_tenberge2PC_positive")
d1_map2<-d1_map[vars2]
d1_map3 <- d1_map2 %>% 
  group_by(iso3c) %>% 
  summarize_all(mean,na.rm=T)
write.csv(d1_map3,"dvs_map_data.csv",row.names = F)
## Remove NAs before importing into QGIS
## download and import into QGIS the World Map shapefile from
## https://gadm.org/data.html 

### Figure 3

vars2<-c("ess_free_elec","ess_civil_rights","ess_women_rights","ess_tax_rich",
         "ess_aid_unemploy","ess_rel_auth", "ess_army_power")
d1de<-d1[vars2]
d1de2<-reshape2::melt(d1de)
d1de2$valuef<-factor(d1de2$value,ordered = TRUE,levels=c("10","9","8","7","6","5","4","3","2","1"))
d1de2$variablen[d1de2$variable=="ess_women_rights"]<-"Gender Equality"
d1de2$variablen[d1de2$variable=="ess_free_elec"]<-"Elections"
d1de2$variablen[d1de2$variable=="ess_civil_rights"]<-"Civil Rights"
d1de2$variablen[d1de2$variable=="ess_aid_unemploy"]<-"Aid Unemployed"
d1de2$variablen[d1de2$variable=="ess_tax_rich"]<-"Tax Rich"
d1de2$variablen[d1de2$variable=="ess_army_power"]<-"Army Rule"
d1de2$variablen[d1de2$variable=="ess_rel_auth"]<-"Religious Authority"


ggplot(d1de2, aes(x = factor(variablen,
                             levels=c("Religious Authority","Army Rule",
                                      "Tax Rich","Aid Unemployed",
                                      "Civil Rights","Elections",
                                      "Gender Equality")), 
                  fill = valuef)) +
  geom_bar(position="fill",width = 0.4,colour = "grey")+
  scale_fill_grey(start = 0.15, end = 0.85,name="Support for \nFeatures of \nDemocracy",na.value="white")+
  labs(x = "", y = "Percent of respondents")+
  scale_y_continuous(labels = function(x) paste0(x*100, "%"))+theme_bw()+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.text.y = element_text(face="plain", vjust=0.5,hjust = 0,
                                   size=12, angle=0),
        axis.title.x=element_text(size=14, margin = margin(t = 20, r = 0, b = 0, l = 0)),
        axis.title.y=element_text(size=14,face="plain"))+
  coord_flip()

ggsave("Figure3.pdf",width = 5,height = 5)
dev.off()

### Figures 4a-c

## recode variables to original scale
d1$ess_free_elec1010<-d1$ess_free_elec10*10
d1$all_non_dem1010<-d1$all_non_dem10*10
d1$redist_dem1010<-d1$redist_dem10*10
d1$lib_dem_no_elec1010<-d1$lib_dem_no_elec10*10
d1$all_dem1010<-d1$all_dem10*10


vars6<-c("all_dem1010")
d1def<-d1[vars6]
d1def2<-reshape2::melt(d1def)

g <- ggplot(d1def2, aes(value,fill = factor(value)))
g + geom_bar(aes(y = (..count..)/sum(..count..)),width = 0.15) + 
  scale_y_continuous(labels = function(x) paste0(x*100, "%"))+theme_bw()+
  scale_x_continuous(breaks= pretty_breaks(10))+
  scale_fill_grey(start = 0.95, end = 0.05,name="none")+
  labs(x = "Support for Concepts of Democracy", y = "Percent of respondents")+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.text.y = element_text(face="plain", vjust=0.5,hjust = 0,
                                   size=12, angle=0),
        axis.title.x=element_text(size=14, margin = margin(t = 20, r = 0, b = 0, l = 0)),
        axis.title.y=element_text(size=14,face="plain"),
        legend.position = "none")
ggsave("Figure4a.pdf",width = 5,height = 5)
dev.off()


vars6<-c("ess_free_elec1010","redist_dem1010","lib_dem_no_elec1010")
d1def<-d1[vars6]
d1def2<-reshape2::melt(d1def)
d1def2$valuef<-factor(d1def2$value,ordered = TRUE,levels=c("10","9.5","9","8.5","8","7.5","7","6.5","6","5.5","5","4.5","4","3.5","3","2.5","2","1.5","1"))

my.labels <- c("Electoral \nDemocracy",
               "Liberal \nDemocracy", 
               "Redistributive \nDemocracy") # first create labels, add \n where appropriate.


ggplot(d1def2, aes(x = factor(variable,
                              levels=c("ess_free_elec1010",
                                       "lib_dem_no_elec1010","redist_dem1010")), 
                   fill = factor(valuef))) +
  geom_bar(position="fill",width = 0.4,colour = "grey")+
  scale_fill_grey(start = 0.05, end = 0.95,name="Support for \nConcepts of \nDemocracy",na.value="white")+
  labs(x = "", y = "Percent of respondents")+
  scale_y_continuous(labels = function(x) paste0(x*100, "%"))+theme_bw()+
  scale_x_discrete(labels= my.labels)+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.text.y = element_text(face="plain", vjust=0.5,hjust = 0,
                                   size=12, angle=0),
        axis.title.x=element_text(size=14, margin = margin(t = 20, r = 0, b = 0, l = 0)),
        axis.title.y=element_text(size=14,face="plain"),
        legend.position = "bottom")
ggsave("Figure4b.pdf",width = 5,height = 5)
dev.off()


vars7<-c("all_non_dem1010")
d1def<-d1[vars7]
d1def2<-reshape2::melt(d1def)
d1def2$valuef<-factor(d1def2$value,ordered = TRUE,levels=c("10","9.5","9","8.5","8","7.5","7","6.5","6","5.5","5","4.5","4","3.5","3","2.5","2","1.5","1"))
d1def2$variablen[d1def2$variable=="all_non_dem1010"]<-"Antidemocratic"


ggplot(d1def2, aes(x = factor(variablen,
                              levels=c("Antidemocratic")), 
                   fill = factor(valuef))) +
  geom_bar(position="fill",width = 0.2,colour = "grey")+
  scale_fill_grey(start = 0.05, end = 0.95,name="Antidemocratic",na.value="white")+
  labs(x = "", y = "Percent of respondents")+
  scale_y_continuous(labels = function(x) paste0(x*100, "%"))+theme_bw()+
  theme(axis.text.x = element_blank(),axis.ticks.x = element_blank(),
        axis.text.y = element_text(face="plain", vjust=0.5,hjust = 0,
                                   size=12, angle=0),
        axis.title.x=element_blank(),
        axis.title.y=element_text(size=14,face="plain"),
        legend.position = "bottom")
ggsave("Figure4c.pdf",width = 5,height = 5)
dev.off()



## fixed effects models with index DV
## Table 1

m1giw<-plm(dem_go_imp_varimax_tenberge2PC_positive~all_non_dem10+
             edu2+income_group2+female+age3+
             relig_attend3+trust_people2+confid_gov2+
             no_human_rights2+party_member2+
             v2x_polyarchy+gdppc_PPP_currentUSD_log+gini_disp100+yearf,
           model = 'within', index = c('V2f'),
           data=d1)
summary(m1giw)


m2giw<-plm(dem_go_imp_varimax_tenberge2PC_positive~all_dem10+
             edu2+income_group2+female+age3+
             relig_attend3+trust_people2+confid_gov2+
             no_human_rights2+party_member2+
             v2x_polyarchy+gdppc_PPP_currentUSD_log+gini_disp100+yearf,
           model = 'within', index = c('V2f'),
           data=d1)
summary(m2giw)


m3giw<-plm(dem_go_imp_varimax_tenberge2PC_positive~ess_free_elec10+
             edu2+income_group2+female+age3+
             relig_attend3+trust_people2+confid_gov2+
             no_human_rights2+party_member2+
             v2x_polyarchy+gdppc_PPP_currentUSD_log+gini_disp100+yearf,
           model = 'within', index = c('V2f'),
           data=d1)
summary(m3giw)


m4giw<-plm(dem_go_imp_varimax_tenberge2PC_positive~lib_dem_no_elec10+
             edu2+income_group2+female+age3+
             relig_attend3+trust_people2+confid_gov2+
             no_human_rights2+party_member2+
             v2x_polyarchy+gdppc_PPP_currentUSD_log+gini_disp100+yearf,
           model = 'within', index = c('V2f'),
           data=d1)
summary(m4giw)


m5giw<-plm(dem_go_imp_varimax_tenberge2PC_positive~redist_dem10+
             edu2+income_group2+female+age3+
             relig_attend3+trust_people2+confid_gov2+
             no_human_rights2+party_member2+
             v2x_polyarchy+gdppc_PPP_currentUSD_log+gini_disp100+yearf,
           model = 'within', index = c('V2f'),
           data=d1)
summary(m5giw)


m6giw<-plm(dem_go_imp_varimax_tenberge2PC_positive~redist_dem10+lib_dem_no_elec10+ess_free_elec10+
             edu2+income_group2+female+age3+
             relig_attend3+trust_people2+confid_gov2+
             no_human_rights2+party_member2+
             v2x_polyarchy+gdppc_PPP_currentUSD_log+gini_disp100+yearf,
           model = 'within', index = c('V2f'),
           data=d1)
summary(m6giw)


texreg(list(m1giw,m2giw,m3giw,m4giw,m5giw,m6giw),booktabs = TRUE, dcolumn = TRUE,
       custom.coef.names = c("Antidemocratic", "Education","Income","Female", 
                             "Age", "Religious Attendance","Trust People",
                             "Trust Government","Human Rights","Party Member",
                             "Democracy","GDP pc (log)","Inequality","2006",
                             "2007", "2009","2010", "2011","2012",
                             "2013","2014","2017","2018","2019",
                             "2020","2021","2022",
                             "Conceptual Breadth",
                             "Electoral Democracy",
                             "Liberal Democracy","Social Democracy"))

## Table 2



m1igiw<-plm(dem_go_imp_varimax_tenberge2PC_positive~all_non_dem10*v2x_polyarchy+
              edu2+income_group2+female+age3+
              relig_attend3+trust_people2+confid_gov2+
              no_human_rights2+party_member2+
              gdppc_PPP_currentUSD_log+gini_disp100+yearf,
            model = 'within', index = c('V2f'),
            data=d1)
summary(m1igiw)


m2igiw<-plm(dem_go_imp_varimax_tenberge2PC_positive~all_dem10*v2x_polyarchy+
              edu2+income_group2+female+age3+
              relig_attend3+trust_people2+confid_gov2+
              no_human_rights2+party_member2+
              gdppc_PPP_currentUSD_log+gini_disp100+yearf,
            model = 'within', index = c('V2f'),
            data=d1)
summary(m2igiw)


m3igiw<-plm(dem_go_imp_varimax_tenberge2PC_positive~ess_free_elec10*v2x_polyarchy+
              edu2+income_group2+female+age3+
              relig_attend3+trust_people2+confid_gov2+
              no_human_rights2+party_member2+
              gdppc_PPP_currentUSD_log+gini_disp100+yearf,
            model = 'within', index = c('V2f'),
            data=d1)
summary(m3igiw)


m4igiw<-plm(dem_go_imp_varimax_tenberge2PC_positive~lib_dem_no_elec10*v2x_polyarchy+
              edu2+income_group2+female+age3+
              relig_attend3+trust_people2+confid_gov2+
              no_human_rights2+party_member2+
              gdppc_PPP_currentUSD_log+gini_disp100+yearf,
            model = 'within', index = c('V2f'),
            data=d1)
summary(m4igiw)


m5igiw<-plm(dem_go_imp_varimax_tenberge2PC_positive~redist_dem10*v2x_polyarchy+
              edu2+income_group2+female+age3+
              relig_attend3+trust_people2+confid_gov2+
              no_human_rights2+party_member2+
              gdppc_PPP_currentUSD_log+gini_disp100+yearf,
            model = 'within', index = c('V2f'),
            data=d1)
summary(m5igiw)


m6igiw<-plm(dem_go_imp_varimax_tenberge2PC_positive~redist_dem10*v2x_polyarchy+
              lib_dem_no_elec10*v2x_polyarchy+
              ess_free_elec10*v2x_polyarchy+
              edu2+income_group2+female+age3+
              relig_attend3+trust_people2+confid_gov2+
              no_human_rights2+party_member2+
              gdppc_PPP_currentUSD_log+gini_disp100+yearf,
            model = 'within', index = c('V2f'),
            data=d1)
summary(m6igiw)


texreg(list(m1igiw,m2igiw,m3igiw,m4igiw,m5igiw,m6igiw),booktabs = TRUE, dcolumn = TRUE,
       stars = c(0.001, 0.01, 0.05, 0.1),
       custom.coef.names = c("Antidemocratic", "Democracy", "Education",
                             "Income","Female", 
                             "Age", "Religious Attendance","Trust People",
                             "Trust Government","Human Rights","Party Member",
                             "GDP pc (log)","Inequality","2006",
                             "2007","2009","2010", "2011","2012",
                             "2013","2014","2017","2018","2019",
                             "2020","2021","2022",
                             "Antidemocratic:Dem",
                             "Conceptual Breadth","Conceptual Breadth:Dem",
                             "Electoral Democracy","Electoral Democracy:Dem",
                             "Liberal Democracy","Liberal Democracy:Dem",
                             "Social Democracy","Social Democracy:Dem",
                             "Dem:Liberal Democracy",
                             "Dem:Electoral Democracy"))


##############################
############################## Marginal effects, Figures 5a-e
##############################

m1p<-lm(dem_go_imp_varimax_tenberge2PC_positive~all_non_dem10+
          edu2+income_group2+female+age3+
          relig_attend3+trust_people2+confid_gov2+
          no_human_rights2+party_member2+
          v2x_polyarchy+gdppc_PPP_currentUSD_log+gini_disp100+yearf+V2f,
        data=d1)
summary(m1p)


m2p<-lm(dem_go_imp_varimax_tenberge2PC_positive~all_dem10+
          edu2+income_group2+female+age3+
          relig_attend3+trust_people2+confid_gov2+
          no_human_rights2+party_member2+
          v2x_polyarchy+gdppc_PPP_currentUSD_log+gini_disp100+yearf+V2f,
        data=d1)
summary(m2p)


m3p<-lm(dem_go_imp_varimax_tenberge2PC_positive~ess_free_elec10+
          edu2+income_group2+female+age3+
          relig_attend3+trust_people2+confid_gov2+
          no_human_rights2+party_member2+
          v2x_polyarchy+gdppc_PPP_currentUSD_log+gini_disp100+yearf+V2f,
        data=d1)
summary(m3p)


m4p<-lm(dem_go_imp_varimax_tenberge2PC_positive~lib_dem_no_elec10+
          edu2+income_group2+female+age3+
          relig_attend3+trust_people2+confid_gov2+
          no_human_rights2+party_member2+
          v2x_polyarchy+gdppc_PPP_currentUSD_log+gini_disp100+yearf+V2f,
        data=d1)
summary(m4p)


m5p<-lm(dem_go_imp_varimax_tenberge2PC_positive~redist_dem10+
          edu2+income_group2+female+age3+
          relig_attend3+trust_people2+confid_gov2+
          no_human_rights2+party_member2+
          v2x_polyarchy+gdppc_PPP_currentUSD_log+gini_disp100+yearf+V2f,
        data=d1)
summary(m5p)



cm1 <- as.data.frame(ggpredict(m1p, terms = c("all_non_dem10 [all]"),
                               ci.lvl = 0.95,type = c("fe"),back.transform = TRUE,typical = "mean",
                               x.as.factor=F))
pm1 <- ggplot(cm1, aes(y=predicted, x=factor(x), color = factor(x)))

pm1 + geom_pointrange(aes(ymin = conf.low, ymax = conf.high),size=0.5,stat = "identity")+
  scale_colour_grey(start = 0.3, end = 0.7)+
  xlab("Antidemocratic")+#scale_fill_grey()+
  ylab("Level of Support for Democracy")+
  ggtitle("")+#coord_flip()+
  scale_x_discrete(breaks = pretty_breaks(10))+
  guides(color="none")+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.title.x=element_text(size=14, margin = margin(t = 20, r = 0, b = 0, l = 0)))+
  theme(
    axis.text.y=element_text(face="plain", vjust=0.5,
                             size=14, angle=0),
    axis.title.y=element_text(size=14))
ggsave(file="Figure5e.pdf",width = 5, height = 5)
dev.off()


cm2 <- as.data.frame(ggpredict(m2p, terms = c("all_dem10 [all]"),
                               ci.lvl = 0.95,type = c("fe"),back.transform = TRUE,typical = "mean",
                               x.as.factor=F))
pm2 <- ggplot(cm2, aes(y=predicted, x=factor(x), color = factor(x)))

pm2 + geom_pointrange(aes(ymin = conf.low, ymax = conf.high),size=0.5,stat = "identity")+
  scale_colour_grey(start = 0.7, end = 0.3)+
  xlab("Conceptual Complexity")+#scale_fill_grey()+
  ylab("Level of Support for Democracy")+
  ggtitle("")+#coord_flip()+
  scale_x_discrete(breaks = pretty_breaks(10))+
  guides(color="none")+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.title.x=element_text(size=13, margin = margin(t = 20, r = 0, b = 0, l = 0)))+
  theme(
    axis.text.y=element_text(face="plain", vjust=0.5,
                             size=14, angle=0),
    axis.title.y=element_text(size=14))
ggsave(file="Figure5a.pdf",width = 5, height = 5)
dev.off()

cm3 <- as.data.frame(ggpredict(m3p, terms = c("ess_free_elec10 [all]"),
                               ci.lvl = 0.95,type = c("fe"),back.transform = TRUE,typical = "mean",
                               x.as.factor=F))

pm3 <-   ggplot(cm3, aes(y=predicted, x=factor(x), color = factor(x)))


pm3 + geom_pointrange(aes(ymin = conf.low, ymax = conf.high),size=0.5,stat = "identity")+
  scale_colour_grey(start = 0.7, end = 0.3)+
  xlab("Electoral Democracy")+#scale_fill_grey()+
  ylab("Level of Support for Democracy")+
  ggtitle("")+#coord_flip()+
  scale_x_discrete(breaks = pretty_breaks(10))+
  guides(color="none")+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.title.x=element_text(size=14, margin = margin(t = 20, r = 0, b = 0, l = 0)))+
  theme(
    axis.text.y=element_text(face="plain", vjust=0.5,
                             size=14, angle=0),
    axis.title.y=element_text(size=14))
ggsave(file="Figure5b.pdf",width = 5, height = 5)
dev.off()


cm4 <- as.data.frame(ggpredict(m4p, terms = c("lib_dem_no_elec10 [all]"),
                               ci.lvl = 0.95,type = c("fe"),back.transform = TRUE,typical = "mean",
                               x.as.factor=F))

pm4 <-   ggplot(cm4, aes(y=predicted, x=factor(x), color = factor(x)))


pm4 + geom_pointrange(aes(ymin = conf.low, ymax = conf.high),size=0.5,stat = "identity")+
  scale_colour_grey(start = 0.7, end = 0.3)+
  xlab("Liberal Democracy")+#scale_fill_grey()+
  ylab("Level of Support for Democracy")+
  ggtitle("")+#coord_flip()+
  scale_x_discrete(breaks = pretty_breaks(10))+
  guides(color="none")+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.title.x=element_text(size=14, margin = margin(t = 20, r = 0, b = 0, l = 0)))+
  theme(
    axis.text.y=element_text(face="plain", vjust=0.5,
                             size=14, angle=0),
    axis.title.y=element_text(size=14))
ggsave(file="Figure5c.pdf",width = 5, height = 5)
dev.off()


cm5 <- as.data.frame(ggpredict(m5p, terms = c("redist_dem10 [all]"),
                               ci.lvl = 0.95,type = c("fe"),back.transform = TRUE,typical = "mean",
                               x.as.factor=F))

pm5 <- ggplot(cm5, aes(y=predicted, x=factor(x), color = factor(x)))
pm5 + geom_pointrange(aes(ymin = conf.low, ymax = conf.high),size=0.5,stat = "identity")+
  scale_colour_grey(start = 0.7, end = 0.3)+
  xlab("Redistributive Democracy")+#scale_fill_grey()+
  ylab("Level of Support for Democracy")+
  ggtitle("")+#coord_flip()+
  scale_x_discrete(breaks = pretty_breaks(10))+
  guides(color="none")+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.title.x=element_text(size=14, margin = margin(t = 20, r = 0, b = 0, l = 0)))+
  theme(
    axis.text.y=element_text(face="plain", vjust=0.5,
                             size=14, angle=0),
    axis.title.y=element_text(size=14))
ggsave(file="Figure5d.pdf",width = 5, height = 5)
dev.off()


#####################
#################### Marginal effects, figures 6a-e
####################

m1pi<-lm(dem_go_imp_varimax_tenberge2PC_positive~
           all_non_dem10*v2x_polyarchy+
           edu2+income_group2+female+age3+
           relig_attend3+trust_people2+confid_gov2+
           no_human_rights2+party_member2+
           gdppc_PPP_currentUSD_log+gini_disp100+yearf+V2f,
         data=d1)
summary(m1pi)


m2pi<-lm(dem_go_imp_varimax_tenberge2PC_positive~
           all_dem10*v2x_polyarchy+
           edu2+income_group2+female+age3+
           relig_attend3+trust_people2+confid_gov2+
           no_human_rights2+party_member2+
           gdppc_PPP_currentUSD_log+gini_disp100+yearf+V2f,
         data=d1)
summary(m2pi)


m3pi<-lm(dem_go_imp_varimax_tenberge2PC_positive~
           ess_free_elec10*v2x_polyarchy+
           edu2+income_group2+female+age3+
           relig_attend3+trust_people2+confid_gov2+
           no_human_rights2+party_member2+
           gdppc_PPP_currentUSD_log+gini_disp100+yearf+V2f,
         data=d1)
summary(m3pi)


m4pi<-lm(dem_go_imp_varimax_tenberge2PC_positive~
           lib_dem_no_elec10*v2x_polyarchy+
           edu2+income_group2+female+age3+
           relig_attend3+trust_people2+confid_gov2+
           no_human_rights2+party_member2+
           gdppc_PPP_currentUSD_log+gini_disp100+yearf+V2f,
         data=d1)
summary(m4pi)


m5pi<-lm(dem_go_imp_varimax_tenberge2PC_positive~
           redist_dem10*v2x_polyarchy+
           edu2+income_group2+female+age3+
           relig_attend3+trust_people2+confid_gov2+
           no_human_rights2+party_member2+
           gdppc_PPP_currentUSD_log+gini_disp100+yearf+V2f,
         data=d1)
summary(m5pi)


ci1 <- as.data.frame(ggpredict(m1pi, terms = c("all_non_dem10 [all]","v2x_polyarchy"),
                               ci.lvl = 0.95,type = c("fe"),back.transform = TRUE,typical = "mean",
                               x.as.factor=F))
ci1$group <- factor(ci1$group, levels=c("0.33", "0.58", "0.84"), 
                    labels=c("Low", "Medium","High"))

pi1 <- ggplot(ci1, aes(y=predicted, x=x, color = group))
pi1 + geom_pointrange(aes(ymin = conf.low, ymax = conf.high),size=0.5,stat = "identity")+
  scale_colour_grey(start = 0.7, end = 0.3,name="Support for\nDemocracy")+
  xlab("Antidemocratic")+#scale_fill_grey()+
  ylab("Individual Support for Democracy")+
  ggtitle("")+
  scale_x_continuous(breaks = pretty_breaks(5))+ 
  guides(color = guide_legend(reverse=TRUE,title="Country\nDemocracy\nLevel"))+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.title.x=element_text(size=14, margin = margin(t = 20, r = 0, b = 0, l = 0)))+
  theme(
    axis.text.y=element_text(face="plain", vjust=0.5,
                             size=12, angle=0),
    axis.title.y=element_text(size=14))
ggsave(file="Figure6e.pdf",width = 5, height = 5)
dev.off()


ci2 <- as.data.frame(ggpredict(m2pi, terms = c("all_dem10 [all]","v2x_polyarchy"),
                               ci.lvl = 0.95,type = c("fe"),back.transform = TRUE,typical = "mean",
                               x.as.factor=F))
ci2$group <- factor(ci2$group, levels=c("0.33", "0.58", "0.84"), 
                    labels=c("Low", "Medium","High"))

pi2 <- ggplot(ci2, aes(y=predicted, x=x, color = group))
pi2 + geom_pointrange(aes(ymin = conf.low, ymax = conf.high),size=0.5,stat = "identity")+
  scale_colour_grey(start = 0.7, end = 0.3,name="Support for\nDemocracy")+
  xlab("Conceptual Complexity")+#scale_fill_grey()+
  ylab("Individual Support for Democracy")+
  ggtitle("")+
  scale_x_continuous(breaks = pretty_breaks(5))+ 
  guides(color = guide_legend(reverse=TRUE,title="Country\nDemocracy\nLevel"))+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.title.x=element_text(size=14, margin = margin(t = 20, r = 0, b = 0, l = 0)))+
  theme(
    axis.text.y=element_text(face="plain", vjust=0.5,
                             size=12, angle=0),
    axis.title.y=element_text(size=14))
ggsave(file="Figure6a.pdf",width = 5, height = 5)
dev.off()



ci3 <- as.data.frame(ggpredict(m3pi, terms = c("ess_free_elec10 [all]","v2x_polyarchy"),
                               ci.lvl = 0.95,type = c("fe"),back.transform = TRUE,typical = "mean",
                               x.as.factor=F))
ci3$group <- factor(ci3$group, levels=c("0.33", "0.58", "0.84"), 
                    labels=c("Low", "Medium","High"))

pi3 <- ggplot(ci3, aes(y=predicted, x=x, color = group))
pi3 + geom_pointrange(aes(ymin = conf.low, ymax = conf.high),size=0.5,stat = "identity")+
  scale_colour_grey(start = 0.7, end = 0.3,name="Support for\nDemocracy")+
  xlab("Electoral Democracy")+
  ylab("Individual Support for Democracy")+
  ggtitle("")+
  scale_x_continuous(breaks = pretty_breaks(5))+ 
  guides(color = guide_legend(reverse=TRUE,title="Country\nDemocracy\nLevel"))+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.title.x=element_text(size=14, margin = margin(t = 20, r = 0, b = 0, l = 0)))+
  theme(
    axis.text.y=element_text(face="plain", vjust=0.5,
                             size=12, angle=0),
    axis.title.y=element_text(size=14))
ggsave(file="Figure6b.pdf",width = 5, height = 5)
dev.off()

ci4 <- as.data.frame(ggpredict(m4pi, terms = c("lib_dem_no_elec10 [all]","v2x_polyarchy"),
                               ci.lvl = 0.95,type = c("fe"),back.transform = TRUE,typical = "mean",
                               x.as.factor=F))
ci4$group <- factor(ci4$group, levels=c("0.33", "0.58", "0.84"), 
                    labels=c("Low", "Medium","High"))

pi4 <- ggplot(ci4, aes(y=predicted, x=x, color = group))
pi4 + geom_pointrange(aes(ymin = conf.low, ymax = conf.high),size=0.5,stat = "identity")+
  scale_colour_grey(start = 0.7, end = 0.3,name="Support for\nDemocracy")+
  xlab("Liberal Democracy")+#scale_fill_grey()+
  ylab("Individual Support for Democracy")+
  ggtitle("")+
  scale_x_continuous(breaks = pretty_breaks(5))+ 
  guides(color = guide_legend(reverse=TRUE,title="Country\nDemocracy\nLevel"))+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.title.x=element_text(size=14, margin = margin(t = 20, r = 0, b = 0, l = 0)))+
  theme(
    axis.text.y=element_text(face="plain", vjust=0.5,
                             size=12, angle=0),
    axis.title.y=element_text(size=14))
ggsave(file="Figure6c.pdf",width = 5, height = 5)
dev.off()


ci5 <- as.data.frame(ggpredict(m5pi, terms = c("redist_dem10 [all]","v2x_polyarchy"),
                               ci.lvl = 0.95,type = c("fe"),back.transform = TRUE,typical = "mean",
                               x.as.factor=F))
ci5$group <- factor(ci5$group, levels=c("0.33", "0.58", "0.84"), 
                    labels=c("Low", "Medium","High"))

pi5 <- ggplot(ci5, aes(y=predicted, x=x, color = group))
pi5 + geom_pointrange(aes(ymin = conf.low, ymax = conf.high),size=0.5,stat = "identity")+
  scale_colour_grey(start = 0.7, end = 0.3,name="Support for\nDemocracy")+
  xlab("Redistributive Democracy")+#scale_fill_grey()+
  ylab("Individual Support for Democracy")+
  ggtitle("")+
  scale_x_continuous(breaks = pretty_breaks(5))+ 
  guides(color = guide_legend(reverse=TRUE,title="Country\nDemocracy\nLevel"))+
  theme(axis.text.x = element_text(face="plain", vjust=0.5,
                                   size=12, angle=0),
        axis.title.x=element_text(size=14, margin = margin(t = 20, r = 0, b = 0, l = 0)))+
  theme(
    axis.text.y=element_text(face="plain", vjust=0.5,
                             size=12, angle=0),
    axis.title.y=element_text(size=14))
ggsave(file="Figure6d.pdf",width = 5, height = 5)
dev.off()


